package com.xb.cloud.gateway.client;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @ClassName: Context
 * @Description:请求服务器端的构建出来的上下文信息 这个对象主要提供对handler 和 中间件框架访问
 * @author: 鲜备科技
 * @date: 2017年12月19日 下午2:27:50
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
public class Context extends AdapterContext {
	private boolean isCheckToken;
	private String contentType;
	private String rtnText;
	private TimeLine timeLine;

	public Context(HttpServletRequest request, HttpServletResponse response, String urlPath, Boolean isCheckToken,
			TimeLine timeLine, String requestIp, RtnInfo rtnInfo) {
		this.setHttpRequest(request);
		this.setHttpResponse(response);
		this.setUrlPath(urlPath);
		this.setCheckToken(isCheckToken);
		this.setTimeLine(timeLine);
		this.setRtnInfo(rtnInfo);
		this.setRequestIp(requestIp);
	}

	/**
	 * 所有异常
	 */
	private List<ExceptionWrapper> exceptionList;

	/**
	 * 返回序列化后的文本信息
	 */
	public String getRtnText() {
		return rtnText;
	}

	/**
	 * 序列化后的文本信息，一般为json,xml或者html结构化的信息
	 * 
	 * @param rtnText
	 */
	public void setRtnText(String rtnText) {
		this.rtnText = rtnText;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setExceptionList(List<ExceptionWrapper> exceptionList) {
		this.exceptionList = exceptionList;
	}

	public List<ExceptionWrapper> getExceptionList() {
		return exceptionList;
	}

	public void setRequestInfo(RequestInfo requestInfo) {
		this.requestInfo = requestInfo;
	}

	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}

	public void setCurrentUserId(long currentUserId) {
		this.currentUserId = currentUserId;
	}

	public void setCurrentUserName(String currentUserName) {
		this.currentUserName = currentUserName;
	}

	public void setRtnInfo(RtnInfo rtnInfo) {
		this.rtnInfo = rtnInfo;
	}

	public void setHttpRequest(HttpServletRequest httpRequest) {
		this.httpRequest = httpRequest;
	}

	public void setHttpResponse(HttpServletResponse httpResponse) {
		this.httpResponse = httpResponse;
	}

	public void setCheckToken(boolean isCheckToken) {
		this.isCheckToken = isCheckToken;
	}

	public boolean isCheckToken() {
		return isCheckToken;
	}

	public void setTimeLine(TimeLine timeLine) {
		this.timeLine = timeLine;
	}

	public TimeLine getTimeLine() {
		return timeLine;
	}

	public void setRequestIp(String requestIp) {
		this.requestIp = requestIp;
	}

	/**
	 * 
	 * @ClassName: WlTimeLine
	 * @Description:记录请求调用各个几点的时间戳，后期用于做接口性能优化的基数数据
	 * @author: 鲜备科技
	 * @date: 2017年12月18日 下午6:11:51
	 * 
	 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
	 *
	 */
	public static class TimeLine {
		private long requestTime;
		private long serviceRequestTime;
		private long serviceResponseTime;

		public TimeLine(long requestTime) {
			this.requestTime = requestTime;
		}

		public long getRequestTime() {
			return requestTime;
		}

		public long getServiceRequestTime() {
			return serviceRequestTime;
		}

		public void setServiceRequestTime(long serviceRequestTime) {
			this.serviceRequestTime = serviceRequestTime;
		}

		public long getServiceResponseTime() {
			return serviceResponseTime;
		}

		public void setServiceResponseTime(long serviceResponseTime) {
			this.serviceResponseTime = serviceResponseTime;
		}

	}

	/**
	 * 把异常加入到exceptionList
	 * 
	 * @param errorType
	 * @param className
	 * @param e
	 */
	public void addWLException(String errorType, String className, Exception e) {
		List<ExceptionWrapper> exceptionList = this.getExceptionList();
		if (null == exceptionList) {
			exceptionList = new ArrayList<ExceptionWrapper>();
			this.setExceptionList(exceptionList);
		}
		exceptionList.add(new ExceptionWrapper(errorType, className, e));
	}

}
